<template>
  <div class="updpwd">
    <div id="tright-content">
      <el-form
        :model="UpdPwd"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="UpdPwdForm"
      >
        <el-form-item label="原密码" prop="userPwd" ref="userPwd">
          <el-input type="password" v-model="UpdPwd.userPwd" placeholder="请输入你的密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="userNewPwd" ref="userNewPwd">
          <el-input type="password" v-model="UpdPwd.userNewPwd" placeholder="请输入你要修改的密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmpwd" ref="confirmpwd">
          <el-input type="password" v-model="UpdPwd.confirmpwd" placeholder="请确认你的密码"></el-input>
        </el-form-item>

        <div id="save" @click="created('ruleForm')">保存</div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.UpdPwd.userNewPwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      UpdPwd: {
        userPwd: "",
        userNewPwd: "",
        confirmpwd: ""
      },
      rules: {
        userPwd: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
          { min: 6, max: 6, message: "密码是六位的", trigger: "blur" }
        ],
        userNewPwd: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 6, message: "密码必须是6位", trigger: "blur" }
        ],
        confirmpwd: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    created: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .put("http://localhost:3000/personal/UpdPwd", {
              userPwd: this.UpdPwd.userPwd,
              userNewPwd: this.UpdPwd.userNewPwd
            })
            .then(res => {
              console.log("查询结果：", res);
              alert(res.data.msg);
            })
            .catch(err => {
              console.log("错误信息：", err);
            });
        } else {
          console.log("error submit!!");
          alert("请输入正确的信息");
          return false;
        }
      });
    }
  }
};
</script>
<style>
.UpdPwdForm {
  width: 400px;
  height: 370px;
  border-radius: 10px;
  padding: 80px 50px 10px 10px;
  background-color: white;
  margin: 0 auto;
}
/* .UpdPwdForm:hover {
  box-shadow: 0 0 10px 6px rgba(160, 146, 146, 0.5);
} */
.updpwd {
  flex: 2;
}
#tright-name {
  border: 1px solid blue;
  margin-top: 20px;
  height: 50px;
  background-color: #ccc;
}

#tright-content {
  /* background-color: #eee; */
  /* height: 550px; */
  border: 2px solid transparent;
  box-shadow: 0 0 6px #ccc;
}
#save {
  text-align: center;
  width: 100px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 4px;
  color: #fff;
  background-color: #d9534f;
  border-color: #d43f3a;
  margin: 30px auto;
}
table {
  margin: 20px auto;
  line-height: 50px;
}
</style>
